package com.heima.admin.controller;


import com.heima.admin.entity.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exceptions.LeadnewsException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.ChannelDto;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 频道信息表 前端控制器
 * </p>
 *
 * @author HM
 * @since 2021-03-31
 */
@RestController
@RequestMapping("/api/v1/channel")
public class AdChannelController {

    @Autowired
    private AdChannelService adChannelService;

    /**
     * @Author: fuzihang
     * @Date: 20:11 2021/3/31
     * @Parms [channelDto]
     * @ReturnType: com.heima.common.dtos.PageResult<com.heima.model.admin.dtos.ChannelDto>
     * @Description: Controller层分页查询
    */
    @PostMapping("/list")
    public PageResult<ChannelDto> findById(@RequestBody ChannelDto channelDto){
        return adChannelService.findByPage(channelDto);
    }

    /**
     * @Author: fuzihang
     * @Date: 16:23 2021/4/1
     * @Parms [adChannel]
     * @ReturnType: com.heima.common.dtos.ResponseResult
     * @Description: 添加频道
    */
    @PostMapping("/save")
    public ResponseResult saveChannel(@RequestBody AdChannel adChannel){
        //
        adChannel.setCreatedTime(new Date());
        boolean b = adChannelService.save(adChannel);
        if (!b){
            //返回自定义异常类
            throw new LeadnewsException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok("保存成功");
        //return  ResponseResult.ok();
    }

    /**
     * @Author: fuzihang
     * @Date: 16:56 2021/4/1
     * @Parms [adChannel]
     * @ReturnType: com.heima.common.dtos.ResponseResult
     * @Description: 修改频道
    */
    @PutMapping("/update")
    public ResponseResult updateChannel(@RequestBody AdChannel adChannel){
        adChannel.setCreatedTime(new Date());
        boolean b = adChannelService.updateById(adChannel);
        if (!b){
            //返回自定义异常类
            throw new LeadnewsException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok("修改成功");
    }
    /**
     * @Author: fuzihang
     * @Date: 17:13 2021/4/1
     * @Parms [id]
     * @ReturnType: com.heima.common.dtos.ResponseResult
     * @Description: 删除频道
    */
    @DeleteMapping("/del/{id}")
    public ResponseResult deleteChannel(@PathVariable(name = "id") Integer id){

        boolean b = adChannelService.removeById(id);
        if (!b){
            //返回自定义异常类
            throw new LeadnewsException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok("删除成功");
    }

    /**
     * @Author: fuzihang
     * @Date: 17:13 2021/4/1
     * @Parms []
     * @ReturnType: com.heima.common.dtos.ResponseResult
     * @Description: 查询全部频道
    */
    @GetMapping("/channels")
    public ResponseResult findAllChannel(){
        List<AdChannel> list = adChannelService.list();
        return ResponseResult.ok("查询成功!");
    }

}

